import datetime
import logging
import random
import sys
import time

import accountApi
import config
import requests

from DB import MySQLUtils


# 获取提交 token
def getPushToken(user, house_id):
    url = f'http://erp.fangline.cn/publish/get-publish-token.json'
    response = requests.post(url, headers=accountApi.getHeadersByUser(user))
    return response.json()

# 提交沟通记录
def pushFollow(user, house_id):
    content = random.choice(config.executorParams["flowerText"])

    token = getPushToken(user, house_id)['token']
    url = f'http://erp.fangline.cn/house/writeFollowUp?houseId={house_id}&content={content}&token={token}&imgs=&deleteTag=true'
    response = requests.post(url, headers=accountApi.getHeadersByUser(user))
    logging.info(f'房源：{house_id} 子账号填写跟进记录:{content},{response.content}')

# 主程序
def main():
    dbUtil = MySQLUtils.DBUtil()
    logging.info(dbUtil.__dict__)

    # 取house
    sql = f"select house_id from house_owner ho where crt_tm BETWEEN '2024-03-22' AND '2024-03-23';"
    house_id = [64346242, 56481475, 56425479, 56413802, 56375607, 56330295, 56330211, 56325378, 56194698, 56152797, 55805689, 55509742, 55464392, 54765139, 54035777, 53882128, 53877938, 53408311, 53372251, 53075183, 53075129, 52972096, 52794319, 52735127, 52584922, 52396505, 51926954, 51873933, 48552841, 48515437, 48258504, 48032189, 47794240, 47242077, 47100500, 47100014, 65331019, 64976061, 63803068,
                63572298, 63504136, 63347394, 63021299, 62951984, 62880594, 62255799, 61950537, 61444338, 61261862, 59184045, 59182125, 59169608, 58889564, 58819066, 58211049, 58208068, 58207942, 57825507, 57773545, 57174879, 56680181, 56436249, 56376621, 55943037, 54848890, 54787867, 54776488, 85429350, 84908477, 84902702, 84792735, 84497766, 84231917, 84003862, 84003461, 83742107, 83691541, 83306417, 83238735, 83033167]

    for house_id in house_id:
        pushFollow('15056403020', house_id)
        # 停顿时间
        # random_number = random.randint(1, 10)
        # time.sleep(random_number)

    logging.info(f"任务完成")
    sys.exit()

# 主函数
if __name__ == '__main__':
    current_date = datetime.datetime.now().strftime("%Y%m%d")
    log_file = f"logs/{current_date}.log"
    config.setup_logging(log_file)

    main()
